use std::sync::Arc;

use axum::extract::State;
use axum::{Json, Router};
use serde_json::Value;
use tokio::sync::Mutex;
use axum::http::StatusCode;
use axum::routing::post;
use http::HeaderMap;

use crate::app_state::AppState;
use crate::controller::point_controller::get_machine_index;
use crate::dto::point_dto::TDSQLQuery;
use crate::util_entity::anyhow_res::{json_success_response, JsonResult};
use crate::util_entity::response::{json_failed, json_success};

pub fn route () -> Router<Arc<Mutex<AppState>>> {
    Router::new()
        .route("/GetXunYouRetBySingleSql", post(GetXunYouRetBySingleSql))
}
pub async fn GetXunYouRetBySingleSql(
    State(app_state): State<Arc<Mutex<AppState>>>,
    headers: HeaderMap,
    Json(payload): Json<Value>,
) -> JsonResult<Value>  {
    let machine_index = get_machine_index(headers);
    let param =  TDSQLQuery::from_json(payload)?;
    let result = app_state.lock().await.cld_workingcondition_repo.get_all_working_conditions_async(machine_index).await;
    Ok(json_success_response(result))
}

